package com.example.invoker.schedule.govee;

import com.biapp.appboard.mapper.biapp.AppPostingMapper;
import com.biapp.appboard.mapper.operation.PostingMapper;
import com.biapp.appboard.pojo.model.installation.PostingTimes;
import com.biapp.common.util.ZonedDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @Author huaiyu yin
 * @Date 2021/6/30 4:05 下午
 * @Version 1.0
 */
@Service
public class PostingSyncService extends BaseSyncService {
    @Autowired
    private AppPostingMapper appPostingMapper;
    @Autowired
    private PostingMapper postingMapper;

    /*
     * 1。从govee获取每天段时间数据
     * 2。校验数据
     * 3。数据处理
     * 4。存表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(String startDate, String endDate) {
        validParam(startDate,endDate);
        Long startTime = ZonedDateUtil.convertToDate(ZonedDateUtil.toStartOfTheDay(this.getStartDate(), ZonedDateUtil.TIMEZONE_ASIN_SHANGHAI)).getTime();
        Long endTime = ZonedDateUtil.convertToDate(ZonedDateUtil.toEndOfTheDay(this.getEndDate(), ZonedDateUtil.TIMEZONE_ASIN_SHANGHAI)).getTime();
        List<PostingTimes> postingInfo = appPostingMapper.getPostingInfo(startTime, endTime);
        assemblyData(postingInfo);
        save(postingInfo);
    }

    @Override
    public void save(Collection entityList) {
        if(entityList.isEmpty()){
            return ;
        }
        postingMapper.batchInsert((List<PostingTimes>) entityList);
    }

    public void assemblyData(List<PostingTimes> postingInfo) {
        if(postingInfo.isEmpty()){
            return ;
        }
        postingInfo.forEach(item->{
            String daily = ZonedDateUtil.convertDateIntToStrDate(item.getDailyDate());
            item.setDailyYear(item.getDailyDate()/10000);
            item.setDailyMonth(ZonedDateUtil.getIntYearMonth(daily));
            item.setDailyWeek(ZonedDateUtil.getIntYearWeek(daily));
        });
    }



}
